package com.gdth.bank.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import rx.Observable;
import rx.Subscriber;

import com.gdth.bank.BankApplication;
import com.gdth.bank.utils.AppUtil;
import com.gdth.bank.utils.SharedPreferencesUtil;
import com.gdth.database.bean.TAppCanshu;
import com.gdth.database.bean.TAppCanshuDao;
import com.gdth.http.method.HttpMethodServlet;
import com.gdth.model.Item;
import com.gdth.utils.GsonUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

/** 
 * @author  Arvin
 * @version 2016-5-27 11:42:50 
 * 
 */
public class TAppCanshuModel {
	
	public Observable<String> getCanshu() {
		List<TAppCanshu> canshuList = new ArrayList<TAppCanshu>();
		
		TAppCanshuDao dao = BankApplication.getDaoSession().getTAppCanshuDao();
		canshuList = dao.loadAll();
		
		if (canshuList != null && canshuList.size() > 0) {
			return getCanshuLocal(canshuList);
		} else 
			return getCanshuOnline();
	}
	/**
	 * 从网络获取
	 * @return
	 */
	public Observable<String> getCanshuOnline() {
		return HttpMethodServlet.getInstance().getCanshu();
	}

	/**
	 * 从网络获取
	 * @return
	 */
	public Observable<String> getjxgzhz(String zh) {

		return HttpMethodServlet.getInstance().getjxgzhz(zh);
	}
	
	/**
	 * 本地数据库获取
	 * @return
	 */
	public Observable<String> getCanshuLocal(final List<TAppCanshu> canshuList){
		return Observable.create(new Observable.OnSubscribe<String>() {

			@Override
			public void call(Subscriber<? super String> arg0) {
				Gson mGson = GsonUtil.getGson();
				
				String json = mGson.toJson(canshuList);
				arg0.onNext(json);
			}
		});
	}
	
	/**
	 * 保存到本地缓存和数据库
	 * @param json
	 */
	public void saveCanshuCache(String json) {
		List<TAppCanshu> mList = new ArrayList<TAppCanshu>();
		JsonArray array = GsonUtil.getJsonArray(json);
		if (array == null)
			return;
		
		for (JsonElement el : array){
			TAppCanshu mTAppCanshu = GsonUtil.getGson().fromJson(el, TAppCanshu.class);
			
			mList.add(mTAppCanshu);
		}
		
		if (mList.size() == 0)
			return;
		
//		保存到缓存
		saveCanshuCache(mList);
//		保存到数据库
		saveCanshu(mList);
	}
	
	/**
	 * 保存到本地缓存
	 * @param mList
	 */
	private void saveCanshuCache(List<TAppCanshu> mList) {
		List<TAppCanshu> zjlxCjkhList = new ArrayList<TAppCanshu>();
		List<TAppCanshu> zjlxZdyxList = new ArrayList<TAppCanshu>();
		List<TAppCanshu> zjlxDzcpList = new ArrayList<TAppCanshu>();
		List<TAppCanshu> jtgxList = new ArrayList<TAppCanshu>();
		List<TAppCanshu> zczlList = new ArrayList<TAppCanshu>();
		List<TAppCanshu> yxzlList = new ArrayList<TAppCanshu>();
		List<TAppCanshu> bflxList = new ArrayList<TAppCanshu>();
		List<TAppCanshu> cplxList = new ArrayList<TAppCanshu>();
		List<TAppCanshu> fjlxList = new ArrayList<TAppCanshu>();
		List<TAppCanshu> khlbList = new ArrayList<TAppCanshu>();
		List<TAppCanshu> zdyxBzList = new ArrayList<TAppCanshu>();
		List<TAppCanshu> sbztList = new ArrayList<TAppCanshu>();
		List<TAppCanshu> gwList = new ArrayList<TAppCanshu>();
		List<TAppCanshu> jjzlList = new ArrayList<TAppCanshu>();
		List<TAppCanshu> cyzyList = new ArrayList<TAppCanshu>();
		List<TAppCanshu> zcmcList = new ArrayList<TAppCanshu>();
		for (TAppCanshu canshu : mList) {
			String bz = canshu.getBz();
			String bm = canshu.getBm();
			String   zt = canshu.getZt()+"";
			if (bz.equals("ZJ_LX") && bm.equals("T_APP_ZJLX"))
				zjlxCjkhList.add(canshu);
			if (bz.equals("ZJ_LX") && bm.equals("T_ZDYXSBB"))
				zjlxZdyxList.add(canshu);
			if (bz.equals("ZJ_LX") && bm.equals("DZCPYW"))
				zjlxDzcpList.add(canshu);
			if (bz.equals(AppUtil.SHARE_DATA_JTGX))
				jtgxList.add(canshu);
			if(bz.equals(AppUtil.SHARE_DATA_CYZY))
				cyzyList.add(canshu);
			if(bz.equals(AppUtil.SHARE_DATA_ZCMC))
				zcmcList.add(canshu);
			if (bz.equals(AppUtil.SHARE_DATA_ZCZL))
				zczlList.add(canshu);
			if (bz.equals(AppUtil.SHARE_DATA_YXZL) && bm.equals("T_ZDYXSBB"))
				yxzlList.add(canshu);
			if (bz.equals(AppUtil.SHARE_DATA_BFLX) && bm.equals("T_APP_BFJL"))
				bflxList.add(canshu);
			if (bz.equals(AppUtil.SHARE_DATA_CPLX) && bm.equals("T_APP_CPXX"))
				cplxList.add(canshu);
			if (bz.equals(AppUtil.SHARE_DATA_FJLX) && bm.equals("T_APP_FJXX"))
				fjlxList.add(canshu);
			if(bz.equals(AppUtil.SHARE_DATA_KHLB) && bm.equals("T_KH_VIP"))
				khlbList.add(canshu);
			if(bz.equals(AppUtil.SHARE_DATA_ZDYX) && bm.equals("T_CKTUOZHAN"))
				zdyxBzList.add(canshu);
			if(bz.equals("SB_ZT") && bm.equals("DZCPYX"))
				sbztList.add(canshu);
			if(bz.equals("GW") && bm.equals("T_APP_GMJJDJ_JC"))
				gwList.add(canshu);
			if(bz.equals("JJZL") && bm.equals("T_APP_GMJJDJ_NY"))
				jjzlList.add(canshu);
				
		}
		
		Gson mGson = GsonUtil.getGson();
		
		if (zjlxCjkhList.size() > 0) {
			String zjlxCjkhJson = mGson.toJson(zjlxCjkhList);
			SharedPreferencesUtil.saveSharePreference(AppUtil.SHARE_Application, AppUtil.SHARE_DATA_ZJLX_CJKH, zjlxCjkhJson);
		}


		if (cyzyList.size() > 0) {
			String cyzyListJson = mGson.toJson(cyzyList);
			SharedPreferencesUtil.saveSharePreference(AppUtil.SHARE_Application, AppUtil.SHARE_DATA_CYZY, cyzyListJson);
		}

		if (zcmcList.size() > 0) {
			String zcmcListJson = mGson.toJson(zcmcList);
			SharedPreferencesUtil.saveSharePreference(AppUtil.SHARE_Application, AppUtil.SHARE_DATA_ZCMC, zcmcListJson);
		}


		if (zjlxZdyxList.size() > 0) {
			String zjlxZdyxJson = mGson.toJson(zjlxZdyxList);
			SharedPreferencesUtil.saveSharePreference(AppUtil.SHARE_Application, AppUtil.SHARE_DATA_ZJLX_ZDYX, zjlxZdyxJson);
		}
		
		if (zjlxDzcpList.size() > 0) {
			String zjlxDzcpJson = mGson.toJson(zjlxDzcpList);
			SharedPreferencesUtil.saveSharePreference(AppUtil.SHARE_Application, AppUtil.SHARE_DATA_ZJLX_DZCP, zjlxDzcpJson);
		}
		
		if (jtgxList.size() > 0) {
			String jtgxJson = mGson.toJson(jtgxList);
			SharedPreferencesUtil.saveSharePreference(AppUtil.SHARE_Application, AppUtil.SHARE_DATA_JTGX, jtgxJson);
		}
		
		if (zczlList.size() > 0) {
			String zczlJson = mGson.toJson(zczlList);
			SharedPreferencesUtil.saveSharePreference(AppUtil.SHARE_Application, AppUtil.SHARE_DATA_ZCZL, zczlJson);
		}
		
		if (yxzlList.size() > 0) {
			String yxzlJson = mGson.toJson(yxzlList);
			SharedPreferencesUtil.saveSharePreference(AppUtil.SHARE_Application, AppUtil.SHARE_DATA_YXZL, yxzlJson);
		}
		if (bflxList.size() > 0) {
			String bflxJson = mGson.toJson(bflxList);
			SharedPreferencesUtil.saveSharePreference(AppUtil.SHARE_Application, AppUtil.SHARE_DATA_BFLX, bflxJson);
		}
		if (cplxList.size() > 0) {
			String cplxJson = mGson.toJson(cplxList);
			SharedPreferencesUtil.saveSharePreference(AppUtil.SHARE_Application, AppUtil.SHARE_DATA_CPLX, cplxJson);
		}
		if (fjlxList.size() > 0) {
			String fjlxJson = mGson.toJson(fjlxList);
			SharedPreferencesUtil.saveSharePreference(AppUtil.SHARE_Application, AppUtil.SHARE_DATA_FJLX, fjlxJson);
		}
		if (khlbList.size() > 0) {
			String khlbJson = mGson.toJson(khlbList);
			SharedPreferencesUtil.saveSharePreference(AppUtil.SHARE_Application, AppUtil.SHARE_DATA_KHLB, khlbJson);
		}
		if (zdyxBzList.size() > 0) {
			String zdyxJson = mGson.toJson(zdyxBzList);
			SharedPreferencesUtil.saveSharePreference(AppUtil.SHARE_Application, AppUtil.SHARE_DATA_ZDYX, zdyxJson);
		}
		if (sbztList.size() > 0) {
			String sbztJson = mGson.toJson(sbztList);
			SharedPreferencesUtil.saveSharePreference(AppUtil.SHARE_Application, AppUtil.SHARE_DATA_SBZT, sbztJson);
		}
		if (gwList.size() > 0) {
			String gwJson = mGson.toJson(gwList);
			SharedPreferencesUtil.saveSharePreference(AppUtil.SHARE_Application, AppUtil.SHARE_DATA_GW, gwJson);
		}
		if (jjzlList.size() > 0) {
			String jjzlJson = mGson.toJson(jjzlList);
			SharedPreferencesUtil.saveSharePreference(AppUtil.SHARE_Application, AppUtil.SHARE_DATA_JJZL, jjzlJson);
		}
		
	}
	
	/**
	 * 保存参数到本地数据库
	 * @param mList
	 */
	private void saveCanshu(List<TAppCanshu> mList){
		TAppCanshuDao dao = BankApplication.getDaoSession().getTAppCanshuDao();
		dao.deleteAll();
		dao.insertInTx(mList);
	}
	
	/**
	 * 获取参数类型
	 * @param
	 * @return
	 */
	public static List<Item> getCanshuList(String json){
		List<Item> mList = new ArrayList<Item>();
		Gson mGson = new Gson();
		
		JsonParser parser = new JsonParser();

		//通过JsonParser对象可以把json格式的字符串解析成一个JsonElement对象
		JsonElement el = parser.parse(json);

		//把JsonElement对象转换成JsonArray
		JsonArray jsonArray = null;
		if(el.isJsonArray()){
			jsonArray = el.getAsJsonArray();
		} else {
			return mList;
		}

		//遍历JsonArray对象
		Iterator<?> it = jsonArray.iterator();
		while(it.hasNext()){
			JsonElement e = (JsonElement)it.next();
			//JsonElement转换为JavaBean对象
			TAppCanshu mZczl = mGson.fromJson(e, TAppCanshu.class);
			
			Item item = new Item();
			item.setKey(mZczl.getLx().toString());
			item.setValue(mZczl.getMc());
			
			mList.add(item);
		}
		
		return mList;
	}
	
	/**
	 * 判断获取到参数
	 */
	public static boolean isUpdateData() {
		String json = SharedPreferencesUtil.getSharePreferencesByKey(BankApplication.mInstance.getBaseContext(), AppUtil.SHARE_Application, AppUtil.SHARE_DATA_YXZL);
		
		if (json == null || json.equals("") || json.equals("[]")) {
			return true;
		}
		
		return false;
	}
}
